/*
 * Copyright 2010 Pierre-Yves Ricau
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.excilys.ptitwrap.activity;

import java.util.Map.Entry;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.excilys.ptitwrap.R;
import com.excilys.ptitwrap.WrapRecipe;

/**
 * Activity used to show a {@link WrapRecipe} sent as an {@link Intent} extra.
 * 
 * @author Pierre-Yves Ricau
 * 
 */
public class ShowRecipe extends Activity {

	public static final String	WRAP_RECIPE_EXTRA	= "wrap_recipe";

	private static final String	TAG					= ShowRecipe.class.getSimpleName();

	/** Called when the activity is first created. */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.show_recipe);

		WrapRecipe wrapRecipe = (WrapRecipe) getIntent().getSerializableExtra(WRAP_RECIPE_EXTRA);

		if (wrapRecipe == null) {
			Log.e(TAG, "wrapRecipe extra is null when starting ShowRecipe");
			Toast.makeText(this, R.string.null_recipe_error, Toast.LENGTH_LONG);
			finish();
			return;
		}

		Spanned recipeContent = formatRecipe(getString(R.string.recipe_content), getString(R.string.recipe_eater), wrapRecipe);

		((TextView) findViewById(R.id.recipe_content)).setText(recipeContent);

	}

	/**
	 * Formats the template parameters with the WrapRecipe content to create the
	 * final text. The template string smay contain Html markup, and
	 * String.format tags.
	 * 
	 * @param template
	 * @param wrapRecipe
	 * @return A displayable text
	 */
	private Spanned formatRecipe(String template, String recipeEaterTemplate, WrapRecipe wrapRecipe) {

		StringBuilder recipeRepresentation = new StringBuilder(String.format(template, //
				wrapRecipe.getNumberOfWraps(), //
				wrapRecipe.getSaladWeight(), //
				wrapRecipe.getSaladPackNb(), //
				wrapRecipe.getBearnaiseWeight(), //
				wrapRecipe.getBearnaisePackNb(), //
				wrapRecipe.getCheeseNb(), //
				wrapRecipe.getCheesePackNb(), //
				wrapRecipe.getTomatoNb(), //
				wrapRecipe.getTortillaNb(), //
				wrapRecipe.getTortillaPackNb(), //
				wrapRecipe.getChickenNuggetNb(), //
				wrapRecipe.getChickenNuggetPackNb()//
				));

		if (wrapRecipe.getWrapEaters() != null) {
			for (Entry<String, Integer> wrapEaters : wrapRecipe.getWrapEaters().entrySet()) {
				recipeRepresentation.append(String.format(recipeEaterTemplate, wrapEaters.getKey(), wrapEaters.getValue()));
			}
		}

		return Html.fromHtml(recipeRepresentation.toString());
	}
}
